﻿namespace Chameleon.Storage
{
	using Microsoft.WindowsAzure.StorageClient;
	using System.IO;

	/// <summary>
	/// Provides a representation of an Azure Storage blob.
	/// </summary>
	
	internal sealed class AzureStorageItem : IStorageItem
	{
		#region Fields

			/// <summary>
			/// Blob item represented by the current instance.
			/// </summary>
			
			CloudBlob Blob;

		#endregion


		#region Methods

			/// <summary>
			/// Stores the specified string content in the storage item.
			/// </summary>
			/// <param name="content">
			/// String content to store.
			/// <para>Content is assumed to be in UTF-8 encoding.</para>
			/// </param>
			
			public void Put( string content )
			{
				Blob.UploadText( content );
			}


			/// <summary>
			/// Stores the specified binary content in the storage item.
			/// </summary>
			/// <param name="content">Byte array of content to store.</param>

			public void Put( byte[] content )
			{
				Blob.UploadByteArray( content );
			}


			/// <summary>
			/// Stores the specified stream content into the storage item.
			/// </summary>
			/// <param name="content">Stream containing content to store.</param>

			public void Put( Stream content )
			{
				Blob.UploadFromStream( content );
			}
			

			/// <summary>
			/// Returns the item's contents as a string.
			/// </summary>

			public string AsString()
			{
				return Blob.DownloadText();
			}


			/// <summary>
			/// Returns the item's contents as a byte array.
			/// </summary>

			public byte[] AsBytes()
			{
				return Blob.DownloadByteArray();
			}


			/// <summary>
			/// Puts the item's contents into a stream.
			/// </summary>
			/// <param name="target">Target stream into which the item's contents will be placed.</param>

			public void AsStream( Stream target )
			{
				Blob.DownloadToStream( target );
			}

		#endregion


		#region Constructors

			/// <summary>
			/// Initializes a new instance to represent the specified cloud storage blob.
			/// </summary>
			/// <param name="blob">Azure storage blob to be represented.</param>
			
			public AzureStorageItem( CloudBlob blob )
			{
				Blob = blob;
			}

		#endregion
	}
}
